package com.quanyan.ic.datacenter.dao.impl;

import com.quanyan.ic.datacenter.dao.PlaceDao;
import com.quanyan.ic.datacenter.http.resp.RespCategoryAlarms;
import com.quanyan.ic.datacenter.mapper.place.TbBizPlaceCategoryMapper;
import com.quanyan.ic.datacenter.mapper.place.TbBizPlaceMapper;
import com.quanyan.ic.datacenter.mapper.place.TbBizPlaceUnitMapper;
import com.quanyan.ic.datacenter.mapper.place.TbCityMapper;
import com.quanyan.ic.datacenter.mapper.place.entity.*;
import com.quanyan.ic.datacenter.model.PlaceUnitInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jian.zhou on 2017/8/24.
 */
@Repository
public class PlaceDaoImpl implements PlaceDao {

    private final static Map<Byte, String> categoryMap = new HashMap<>();

    static {
        categoryMap.put((byte)1, "篮球");
        categoryMap.put((byte)2, "羽毛球");
        categoryMap.put((byte)3, "足球");
        categoryMap.put((byte)4, "网球");
        categoryMap.put((byte)5, "乒乓球");
        categoryMap.put((byte)6, "健身");
        categoryMap.put((byte)7, "瑜伽");
        categoryMap.put((byte)8, "桌球");
        categoryMap.put((byte)9, "壁球");
        categoryMap.put((byte)10,"气排球" );
    }

    @Autowired
    private TbBizPlaceCategoryMapper tbBizPlaceCategoryMapper;

    @Autowired
    private TbBizPlaceMapper tbBizPlaceMapper;

    @Autowired
    private TbBizPlaceUnitMapper tbBizPlaceUnitMapper;

    @Autowired
    private TbCityMapper tbCityMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public TbBizPlace findTbBizPlace(Integer placeId){//todo add redis
        if ( placeId != null ){
            return tbBizPlaceMapper.selectByPrimaryKey(placeId);
        }
        return null;
    }

    @Override
    public List<RespCategoryAlarms> findPlaceCategory(Integer placeId){
        TbBizPlaceCategoryExample example = new TbBizPlaceCategoryExample();
        TbBizPlaceCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andPlaceIdEqualTo(placeId);
        List<TbBizPlaceCategory> list = tbBizPlaceCategoryMapper.selectByExample(example);
        List<RespCategoryAlarms> results = new ArrayList<>();
        if ( list != null && list.size() > 0){
            for (TbBizPlaceCategory db : list ){
                RespCategoryAlarms respCategoryAlarms = new RespCategoryAlarms();
                respCategoryAlarms.setCategoryId(db.getCategoryId());
                results.add(respCategoryAlarms);
            }
        }
        return results;
    }

    @Override
    public PlaceUnitInfo getPlaceUnitInfo(Integer placeUnitId) {//TODO add redis
        if (placeUnitId == null) {
            return null;
        }
        TbBizPlaceUnit tbBizPlaceUnit = tbBizPlaceUnitMapper.selectByPrimaryKey(placeUnitId);
        if (tbBizPlaceUnit == null){
            return null;
        }
        TbBizPlace tbBizPlace = this.findTbBizPlace(tbBizPlaceUnit.getPlaceId());
        if (tbBizPlace == null){
            return null;
        }
        PlaceUnitInfo placeUnitInfo = new PlaceUnitInfo();
        placeUnitInfo.setPlaceUnitId(placeUnitId);
        placeUnitInfo.setPlaceUnitName(tbBizPlaceUnit.getName());
        placeUnitInfo.setCategoryId((int)tbBizPlaceUnit.getCategoryId());
        placeUnitInfo.setCategoryName(categoryMap.get(tbBizPlaceUnit.getCategoryId()));
        placeUnitInfo.setPlaceId(tbBizPlaceUnit.getPlaceId());
        placeUnitInfo.setPlaceName(tbBizPlace.getName());
        placeUnitInfo.setCityId(tbBizPlace.getCityId());
        TbCity tbCity = tbCityMapper.selectByPrimaryKey(tbBizPlace.getCityId());
        if (tbCity != null ) {
            placeUnitInfo.setCityName(tbCity.getName());
        }
        return placeUnitInfo;
    }

    public String getTbCity(Integer cityId){
        String name = "";
        TbCity tbCity = tbCityMapper.selectByPrimaryKey(cityId);
        if (tbCity != null)
            name = tbCity.getName();
        return name;
    }
}
